define(['jquery', 'bootstrap', 'backend', 'table', 'form','html2canvas'], function ($, undefined, Backend, Table, Form,html2canvas) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'broadcast/broadcast/index' + location.search,
                    add_url: 'broadcast/broadcast/add',
                    edit_url: 'broadcast/broadcast/edit',
                    del_url: 'broadcast/broadcast/del',
                    multi_url: 'broadcast/broadcast/multi',
                    import_url: 'broadcast/broadcast/import',
                    table: 'broadcast',
                }
            });

            var table = $("#table");
            function getBase64(){
                //https://oss.bigchun.com/xiangce/uploads/20230529/daec6f15d49eddf4372251e27a9e7699.png?x-oss-process=image/resize,w_750
                fetch('https://oss.bigchun.com/xiangce/uploads/20230524/69a43d797792a4f664bb50d6ace77dea.jpg?x-oss-process=image/resize,w_750',{
                    responseType:'blob',
                    mode:'no-cors'
                }).then(response=>{
                    return response.blob()
                }).then(blob=>{
                    console.log(blob);
                    let oFileReader = new FileReader();
                    oFileReader.onloadend = function(e){
                        console.log(e);
                        console.log(e.target.result)
                    },
                    oFileReader.readAsDataURL(blob)
                })
            }
            getBase64();
            function takeScreenshot(){
                html2canvas(document.getElementById('photodown')).then(canvas=>{convertCanvasToImg(canvas)})
            }
            function convertCanvasToImg(canvas){
                var myBlob = dataURLtoBlob(canvas.toDataURL('image/jpeg',0.6));
                myUrl = URL.createObjectURL(myBlob);
                downImg(myUrl);
            }
            function dataURLtoBlob(dataurl){
                var arr = dataurl.split(','),mime='image/jpeg',

                bstr=atob(arr[1]),n=bstr.length,u8arr=new Uint8Array(n);
                console.log(mime);
                while(n--){
                    u8arr[n]=bstr.charCodeAt(n);
                }
                return new Blob([u8arr],{type:mime});
            }
            function downImg(url){
                let a=document.createElement('a')
                let clickEvent = document.createEvent('MouseEvents');
                a.setAttribute('href',url);
                a.setAttribute('download','share')
                a.setAttribute("target",'blank')
                clickEvent.initEvent('click',true,true)
                a.dispatchEvent(clickEvent);
            }
            $(document).on('click','#downID',function(){
                takeScreenshot();
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'title', title: '主题标题', operate: 'LIKE'},
                        {field: 'image', title: 'banner',events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'width', title: '默认宽度', operate: false},
                        {field: 'browse', title: '浏览', operate: false},
                        {field: 'zan', title: '点赞', operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                        buttons: [
                            {
                                name: 'qrcode',
                                title:'小程序二维码',
                                classname: 'btn btn-xs btn-warning btn-click',
                                icon: 'fa fa-share-alt-square',
                                click:function(e, value, row, index){
                                    let shareurl = Config.domain_url+'/root.php/ajax/share?id='+value.id
                                    let qrcodeurl = Config.domain_url+'/addons/qrcode/index/build?text='+Config.domain_url+'/broadcast/'+value.id;
                                    layer.open({
                                        type: 1,
                                        area: ['600px', '770px'], // 宽高
                                        title: false, // 不显示标题栏
                                        closeBtn: 0,
                                        shadeClose: true, // 点击遮罩关闭层
                                        content: '<div id="photodown" style="background:#fff;"><div style="width:600px;height:320px;overflow:hidden;"><img src="'+shareurl+'" style="width:600px;height:auto;"></div><div style="padding:20px 50px;font-size:32px;height:70px;line-height:30px;color:#000">'+value.title+'</div><div style="padding:0 10px;text-align:center;"><hr><img src="'+qrcodeurl+'" style="width:240px;height:240px;display:block;padding:15px auto;margin:0 auto;"><div>[长按识别二维码，观看现场图片直播]</div><br><br></div></div><button class="btn btn-success" type="button" style="width:600px;" id="downID" title="下载分享图">下载</button>',
                                      });

                                    return false;
                                }
                                
                            },
                            {
                                name:'sourceAdd',
                                title:'添加原图',
                                classname:'btn btn-xs btn-primary btn-dialog',
                                icon:'fa fa-plus',
                                url:'broadcast/source/add',
                            },
                            {
                                name:'sourceshow',
                                title:'原图列表',
                                classname:'btn btn-xs btn-primary btn-dialog sourceshow',
                                icon:'fa fa-photo',
                                url: function (row) {
                                    return "broadcast/source/index?bcid="+row.id;
                                },
                            },
                            {
                                name:'site',
                                title:'设置',
                                classname:'btn btn-xs btn-success btn-dialog',
                                icon:'fa fa-gear',
                                url:'broadcast/broadcast/site',
                            },{
                                name:'momentAdd',
                                title:'添加精彩瞬间',
                                classname:'btn btn-xs btn-warning btn-dialog',
                                icon:'fa fa-plus',
                                url:'broadcast/moment/add',
                            },
                            {
                                name:'momentshow',
                                title:'精彩瞬间列表',
                                classname:'btn btn-xs btn-warning btn-dialog sourceshow',
                                icon:'fa fa-photo',
                                url: function (row) {
                                    return "broadcast/moment/index?bcid="+row.id;
                                },
                            },
                        ],
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('post-body.bs.table',function(){
                $(".sourceshow").data("area",["100%","100%"]);
            })
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        site:function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
